import java.util.Arrays;

public class Solution1320 {
    public int minimumDistance(String word) {
        int[] f=new int[26];
        for (int i=1;i<word.length();i++){
            int[] g=new int[26];
            for (int j=0;j<26;j++){
                g[j]=Integer.MAX_VALUE;
                for (int k=0;k<26;k++){
                    g[j]=Math.min(g[j],f[k]+deal(word.charAt(i-1),(char)(k+'A'),word.charAt(i),(char)(j+'A')));
                }
            }
            f=g;
        }
        return Arrays.stream(f).min().getAsInt();
    }

    private int deal(char a, char b, char c, char d) {
        int[] aa= {(a-'A')/6,(a-'A')%6};
        int[] bb= {(b-'A')/6,(b-'A')%6};
        int[] cc= {(c-'A')/6,(c-'A')%6};
        int[] dd= {(d-'A')/6,(d-'A')%6};
        return Math.min(Math.abs(aa[0]-cc[0])+Math.abs(aa[1]-cc[1])+Math.abs(bb[0]-dd[0])+Math.abs(bb[1]-dd[1]),
                Math.abs(aa[0]-dd[0])+Math.abs(aa[1]-dd[1])+Math.abs(bb[0]-cc[0])+Math.abs(bb[1]-cc[1]));
    }

    public static void main(String[] args) {
        System.out.println(new Solution1320().minimumDistance("CAKE"));
    }
}
